<link rel="stylesheet" type="text/css" href="/assets/third-party/datatables-1.10.0/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="/assets/css/dataTables.bootstrap.css">
<link rel="stylesheet" type="text/css" href="/assets/third-party/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css">
<div class="container-fluid" style="margin-top: 60px;">
    <div class="row" style="margin-top: 10px;">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    应用列表
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <?php if(\Input::get('action', false)){ ?>
                    <select id="category_id" class="form-control" style="width: 200px; margin-bottom: 20px;">
                    </select>
                    <?php } ?>                    
                    <table class="table table-bordered table-hover" role="dataTables">
                        <thead>
                            <tr>
                                <th style="width: 30px;">编号</th>
                                <th style="width: 80px;">应用名称</th>
                                <th style="width: 90px;">应用分类</th>
                                <!--<th style="width: 40px;">角标</th>
                                <th style="width: 60px;">作者/公司</th>
                                <th style="width: 60px;">支持分成</th>
                                <th style="width: 60px;">版本</th>-->
                                <th style="width: 160px;">标签</th>
                                <th style="width: 60px;">下载次数</th>
                                <th style="width: 60px;">下载地址</th>
                                <th style="width: 80px;">入库时间</th>
                                <th style="width: 80px;">更新时间</th>
                                <th style="width: 120px;">操作</th>
                            </tr>
                        </thead>
                        <tbody id="apps">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="/assets/js/tools.js"></script>
<script type="text/javascript" src="/assets/third-party/datatables-1.10.0/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/assets/js/dataTables.bootstrap.js"></script>
<script type="text/javascript">
    $(function(){

        $('table[role="dataTables"]').dataTable({
            "serverSide": true, 
            "ajax": '/admin/app<?php echo \Input::get('action', false) ? "?action=" . \Input::get('action') . (\Input::get('category_id', false) ? "&category_id=" . \Input::get('category_id') : '') : '';?>',
            "columns": [
                { "data": "id" },
                { "data": "name" },
                { "data": "categoryName" },
                /*{ "data": "boxLabel" },
                { "data": "developer" },
                { "data": "incomeShare" },
                { "data": "versionName" },*/
                { "data": "tag" },
                { "data": "downloadTimes" },
                { "data": "downloadUrl" },
                { "data": "createTime" },
                { "data": "updateTime" },
                { "data": "options" },
            ],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "很抱歉，未找到相关数据",
                "sInfo": "从 _START_ 至 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有符合条件的数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "sSearch": "搜索：",
                "oPaginate": {  
                    "sFirst": "首页",  
                    "sPrevious": "上一页",
                    "sNext": "下一页",
                    "sLast": "末页"
                }  
            }
        });
        
        $('#category_id').change(function(){
            window.location.href = "/admin/app<?php echo \Input::get('action', false) ? "?action=" . \Input::get('action') : '';?>" + "&category_id=" + $(this).val();
        });

        $(document).delegate('a[role="property"]', 'click', function(){
            var obj = $(this);
            $.post('/admin/app/property', 
                {
                    app_id: $(this).parents('tr').find('td:eq(0)').text().trim(),
                    category_id: $(this).attr('data-id')
                },
                function(data, status){
                    if(data.status == 'err'){
                        alert(data.msg);
                        return;
                    }
                    alert('设置成功');
                }, 'json');
        });

        $(document).delegate('a[role="del"]', 'click', function(){
            var obj = $(this);
            $.get('/admin/app/del?app_id=' + obj.attr('data-id') + '&category_id=' + $('#category_id').val(), 
                function(data, status){
                    if(data.status == 'err'){
                        alert(data.msg);
                        return;
                    }
                    obj.parents('tr').remove();
                }, 'json');
        });

        init();
    });

    function init(){
        <?php if(\Input::get('action', false)){ ?>
        $.get('/admin/app/cats?module=<?php echo $module;?>',
            function(data, status){
                if(data.status == 'succ'){
                    var items = data.data;
                    for (var i = 0; i < items.length; i++) {
                        $('#category_id').append("<option value='" + items[i].id + "' " + (<?php echo \Input::get('category_id', false) ? \Input::get('category_id') : '0' ?> == items[i].id ? " selected='selected'" : '') + ">" + items[i].name + "</option>");
                    }
                }
                
            }, 'json');
        <?php } ?>
    }
</script>